package com.lanou3g.hw0518.model;

import com.lanou3g.hw0518.pojo.User;
import com.lanou3g.hw0518.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/21;
 * Otherwise, I don't know who created it either
 */

public class RegisterModel {
    public boolean register(User user) {
        Connection conn = JDBCUtil.getConn();
        try {
            // 开启事务
         //   conn.setAutoCommit(false);
            String sqlInsert =
                    "INSERT INTO user (username,pwd,nickname" +
                            ",gender) VALUES (?,?,?,?)";
            PreparedStatement psInsert
                    = conn.prepareStatement(sqlInsert);
            psInsert.setString(1, user.getUsername());
            psInsert.setString(2, user.getPassword());
            psInsert.setString(3, user.getNickName());
            psInsert.setString(4, user.getGender());
            psInsert.executeUpdate();
            psInsert.close();// 直接关闭

            // 查询刚刚插入的主键
            String getIDSQL = "SELECT MAX(uid) FROM user";
            PreparedStatement psID = conn.prepareStatement(getIDSQL);
            ResultSet rs = psID.executeQuery();
            rs.next();
            int uid = rs.getInt(1);
            rs.close();
            psID.close();

            // 查询爱好的 id 然后 插入到 user_hobby表中
            String hobbySQL = "SELECT hid FROM hobby WHERE opt = ?";
            String hobbyInsertSQL
                    = "INSERT INTO user_hobby (hid,uid) VALUES (?,?)";
            PreparedStatement hobbyPs
                    = conn.prepareStatement(hobbySQL);
            PreparedStatement hobbyInsertPs
                    = conn.prepareStatement(hobbyInsertSQL);

            for (String hobby : user.getHobbies()) {
                hobbyPs.setString(1, hobby);
                ResultSet hobbyRs = hobbyPs.executeQuery();
                hobbyRs.next();
                int hid = hobbyRs.getInt(1);
                hobbyInsertPs.setInt(1,hid);
                hobbyInsertPs.setInt(2,uid);
                hobbyInsertPs.executeUpdate();
                hobbyRs.close();
            }
            hobbyInsertPs.close();
            hobbyPs.close();

            // 提交事务
          //  conn.commit();

            return true;// 注册成功
        } catch (Exception e) {
            try {
                // 在catch中回滚
                // 如果哪条sql 抛异常了,我们可以回滚到
                // sql执行之前
                conn.rollback();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return false;
    }

    public boolean hasThisName(String username) {
        String sql = "SELECT count(*) FROM user" +
                " WHERE username = ?";
        Connection conn = JDBCUtil.getConn();
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet resultSet = ps.executeQuery();
            resultSet.next();
            int count = resultSet.getInt(1);

            resultSet.close();
            ps.close();

            return count > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
